package com.yfp.device.service.impl;

import com.yfp.device.domain.AdPushRecord;
import com.yfp.device.domain.MachineAd;
import com.yfp.device.domain.R;
import com.yfp.device.domain.dto.SyncMachineAdResponse;
import com.yfp.device.domain.vo.MachineAdVo;
import com.yfp.device.mapper.AdPushRecordMapper;
import com.yfp.device.mapper.MachineAdMapper;
import com.yfp.device.service.AdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 广告服务
 */
@Service
@Slf4j
public class AdServiceImpl implements AdService {

    private final MachineAdMapper machineAdMapper;

    private final AdPushRecordMapper adPushRecordMapper;

    public AdServiceImpl(MachineAdMapper machineAdMapper, AdPushRecordMapper adPushRecordMapper) {
        this.machineAdMapper = machineAdMapper;
        this.adPushRecordMapper = adPushRecordMapper;
    }

    /**
     * 机器广告设置反馈
     *
     * @param recordNo  广告推送记录号
     * @param setStatus 广告设置状态  1:成功 0：失败
     * @return 响应体
     */
    @Override
    public R<?> machineAdFeedback(String recordNo, Integer setStatus) {
        try {
            AdPushRecord record = new AdPushRecord();
            record.setRecordNo(recordNo);
            record.setSetStatus(setStatus);
            int result = adPushRecordMapper.updateByPrimaryKey(record);
            if (result != 1) {
                log.error("推送记录状态更新失败");
                return R.fail("推送记录状态更新失败");
            }
            // 设置成功，更新机器广告内容
            if (setStatus == 1) {
                AdPushRecord adPushRecord = adPushRecordMapper.selectByPrimaryKey(recordNo);
                MachineAd machineAd = machineAdMapper.selectByMachineIdAndAdId(adPushRecord.getMachineId(), adPushRecord.getAdId());
                if (machineAd == null) {
                    machineAd = new MachineAd();
                    machineAd.setCreateTime(new Date());
                    extracted(machineAd, adPushRecord);
                    machineAdMapper.insertSelective(machineAd);
                } else {
                    extracted(machineAd, adPushRecord);
                    machineAdMapper.updateByPrimaryKey(machineAd);
                }
            }
            return R.ok();
        } catch (Exception e) {
            log.error("机器广告设置反馈失败", e);
            return R.fail();
        }
    }

    /**
     * 同步设备广告
     *
     * @param machineSn 机器编号
     * @return 广告信息
     */
    @Override
    public R<SyncMachineAdResponse> syncMachineAd(String machineSn) {
        try {
            SyncMachineAdResponse syncMachineAdResponse = new SyncMachineAdResponse();
            syncMachineAdResponse.setMachineSn(machineSn);
            List<MachineAdVo> machineAdVos = machineAdMapper.selectMachineAd(machineSn);
            syncMachineAdResponse.setMachineAds(machineAdVos);
            log.info("同步设备广告成功,参数:{},结果:{}",machineSn,syncMachineAdResponse);
            return R.ok(syncMachineAdResponse, "同步设备广告成功");
        } catch (Exception e) {
            log.error("同步设备广告失败", e);
            return R.fail();
        }
    }

    private void extracted(MachineAd machineAd, AdPushRecord adPushRecord) {
        machineAd.setMachineId(adPushRecord.getMachineId());
        machineAd.setAdId(adPushRecord.getAdId());
        machineAd.setUpdateTime(new Date());
        // 1：待播放 2：播放中 3：已结束
        machineAd.setPalyStatus(1);
        machineAd.setPalyAdPosition(adPushRecord.getPalyAdPosition());
        machineAd.setAdType(adPushRecord.getAdType());
        machineAd.setPalyBeginTime(adPushRecord.getPalyBeginTime());
        machineAd.setPlayEndTime(adPushRecord.getPlayEndTime());
    }
}
